<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Grid;
use Illuminate\Http\Request;
use App\Models\GridArea;
use App\Models\GridHouse;
use App\Models\Villager;
use App\Models\ProtocolSet;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Validator;

// 选择户型
class GridHouseController extends Controller
{
    // 录入
    public function addHouse(Request $request)
    {
        $validate = Validator::make($request->all(), [
            'grid_id' => 'required',
            'house_type' => 'required',
        ], [
            'grid_id.required' => '请输入网格id',
            'house_type.required' => '请输入户型数据',
        ]);

        if ($validate->fails()) {
            return showError($validate->errors()->first());
        }

        $params = $request->all();

        try {
            $house_type_arr=json_decode($params['house_type'],true);
            $del=GridHouse::query()
                 ->where('grid_id',$params['grid_id'])
                 ->where('type',1)
                 ->whereNotIn('id',array_column($house_type_arr,'id'))
                 ->delete();
            foreach($house_type_arr as $v){
                $house_data['house_type_id']=$v['house_type_id'];
                $house_data['is_decorate']=$v['is_decorate'];
                $house_data['villager_ids']=$v['villager_ids'];
                $house_data['do_user_id']=auth('api')->user()->id??0;
                if(isset($v['id'])){
                    $update=GridHouse::query()->where('id',$v['id'])->update($house_data);
                }else{
                    $house_data['grid_id']=$params['grid_id'];
                    $house_data['type']=1;
                    $add=GridHouse::query()->create($house_data);
                }
            }

            Grid::query()->where('id',$params['grid_id'])->update(['house_status'=>1]);
            return showSuccess('编辑成功！');
        } catch (\Exception $e) {
            return showError($e->getMessage());
        }
       
    }

    // 回显
    public function houseList(Request $request)
    {
        $validate = Validator::make($request->all(), [
            'grid_id' => 'required',
        ], [
            'grid_id.required' => '请输入网格id',
        ]);

        if ($validate->fails()) {
            return showError($validate->errors()->first());
        }

        $params = $validate->getData();

        $type=$this->getAreaType($params['grid_id']);

        if($type!=1&&$type!=2){
            return $type;
        }

        $house_status=Grid::query()->where('id',$params['grid_id'])->value('house_status');

        if($house_status==1){//已选
            $grid_house_type=1;
        }else{//未选(带入预选户型)
            $grid_house_type=2;
        }

        $data=Grid::query()
        ->select('id','name','house_use','supply_form','build_form','street_id','village_id','project_id','segment_id','if_sign','for_user_id')
        ->where('id',$params['grid_id'])
        ->with('villager:id,name,identity_card,phone,grid_id')
        ->with(['grid_area'=> function ($query) use ($type) {
            $query->select('supply_area','reward_area','grid_id')->where('type', $type);
        }])
        ->with(['grid_house' => function ($query) use ($grid_house_type) {
            $query->where('type', $grid_house_type)->with('house_type:id,name,area');
        }])
        ->first();

        foreach($data['grid_house'] as &$v){
            $v['villager']=Villager::query()->select('id','name')->whereIn('id',explode(',',$v['villager_ids']))->get()->toArray();
        }

        $data['grid_house_type']=$grid_house_type;

        $data['protocol_set']=ProtocolSet::query()
        ->select('id','reward_area','improve_area','public_area_scale','upper_lean','lower_lean')
        ->where('supply_form',$data['supply_form'])
        ->where('build_form',$data['build_form'])
        ->where('street_id',$data['street_id'])
        ->where('village_id',$data['village_id'])
        ->where('project_id',$data['project_id'])
        ->where('segment_id',$data['segment_id'])
        ->first();

        if(empty($data['protocol_set'])){
            return showError('未查询到配置信息，请联系管理员到后台配置');
        }

        return showData($data);
    }

}

